package com.zhongzhi.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.dao.MeasureDao;
import com.zhongzhi.dao.TypeDao;
import com.zhongzhi.service.TypeService;
import com.zhongzhi.springboot.pojo.Aisle;
import com.zhongzhi.springboot.pojo.Bo;
import com.zhongzhi.springboot.pojo.Type;

@Service
public class TypeServiceImpl implements TypeService{

	@Autowired
	private TypeDao typeDao;
	@Autowired
	private MeasureDao measureDao;
	//增
	@Override
	public Bo insert(Type type) {
		//参数校验
		if (type.getTypeName()==null || type.getTypeName().length()==0) {
			return new Bo(false,"机型名称为空",0,null);
		}else if (type.getLengthTime()==null || type.getLengthTime()==0.0) {
			return new Bo(false,"默认时间为空",0,null);
		}else if (type.getWeight()==null || type.getWeight()==0.0) {
			return new Bo(false,"默认负重为空",0,null);
		}else if (type.getStandardA()==null || type.getStandardA()==0.0) {
			return new Bo(false,"默认参数A为空",0,null);
		}else if (type.getStandardB()==null || type.getStandardB()==0.0) {
			return new Bo(false,"默认参数B为空",0,null);
		}else {
			int a = typeDao.insert(type);
			if(a==0) {
				return new Bo(false,"插入数据失败",0,null);
			}
			return new Bo(true,"",a,null);
		}
	}

	//改
	@Override
	public Bo update(Type type) {
		//参数校验
		if (type.getTypeName()==null || type.getTypeName().length()==0) {
			return new Bo(false,"机型名称为空",0,null);
		}else if (type.getLengthTime()==null || type.getLengthTime()==0.0) {
			return new Bo(false,"默认时间为空",0,null);
		}else if (type.getWeight()==null || type.getWeight()==0.0) {
			return new Bo(false,"默认负重为空",0,null);
		}else if (type.getStandardA()==null || type.getStandardA()==0.0) {
			return new Bo(false,"默认参数A为空",0,null);
		}else if (type.getStandardB()==null || type.getStandardB()==0.0) {
			return new Bo(false,"默认参数B为空",0,null);
		}else {
			int a = typeDao.update(type);
			if(a==0) {
				return new Bo(false,"插入数据失败",0,null);
			}
			return new Bo(true,"",a,null);
		}
	}

	//删
	@Override
	public Bo delete(Integer id) {
		if(id<=0) {
			return new Bo(false,"通道选择有误",0,null);
		}
		//根据通道id查询数据,判断这个通道有无数据
		int a = measureDao.selectMeasureId(id);
		if(a>0) {
			return new Bo(false,"当前机型有测量数据,无法删除",0,null);
		}else {
			int b = typeDao.delete(id);
			if(b==0) {
				return new Bo(false,"删除机型数据失败",0,null);
			}
			return new Bo(true,"",b,null);
		}
	}

	//分页查询
	@Override
	public Bo select(Integer pages, Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		pages=pages*line;
		List<Type> list = typeDao.select(pages,line);
		return new Bo(true, "",list.size(),list);

	}

	//获取总记录数
	@Override
	public Bo selectSize() {
		int a = typeDao.selectSize();
		return new Bo(true, "",a,null);
	}

	//按照指定id查询
	@Override
	public Bo selectById(Integer id) {
		//数据校验
		if (id<=0) {
			return new Bo(false, "机型id有误",0,null);
		}else {
			Type type = typeDao.selectById(id);
			if (type==null) {
				return new Bo(false, "无此id数据",0,null);
			}else {
				return new Bo(true, "",10,type);
			}
		}
	}

	//根据名称模糊查询
	@Override
	public Bo selectByDeviceNum(String typeName, Integer pages,Integer line) {
		if (typeName==null || typeName.length()==0) {
			return new Bo(false,"机型名称为空",0,null);
		}//数据校验
		//页数
		if(pages==null || pages<=0) {
			pages = 1;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		//获取总记录数
		pages=pages*line;
		List<Type> list = typeDao.selectByDeviceNum(typeName,pages,line);
		return new Bo(true, "",list.size(),list);
	}

	@Override
	public Bo selectByDeviceNum(String typeName) {
		int a = typeDao.selectByDeviceNumSize(typeName);
		return new Bo(true, "",a,null);
	}



}
